package com.css.sword.knowing.search.dbutil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DbUtil {

    private static String driver;
    private static String url;
    private static String userName;
    private static String password;

    public static Connection getConnection() {
        Connection con = null;
        try {
            getProperties();
            Class.forName(driver);

            con = DriverManager.getConnection(url, userName, password);// 创建数据连接

        } catch (Exception e) {
            System.out.println("数据库连接失败" + e.getMessage());
        }
        return con; // 返回所建立的数据库连接
    }

    private static void getProperties() throws IOException {
        Properties p = new Properties();
        p.load(DbUtil.class.getClassLoader().getResourceAsStream("db.properties"));

        driver = p.getProperty("driverclass");

        url = p.getProperty("url");
        userName = p.getProperty("username");
        password = p.getProperty("password");

    }

}
